package com.tanquandan.askbackend.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.tanquandan.askbackend.dto.R;
import com.tanquandan.askbackend.entity.Chat;
import com.tanquandan.askbackend.service.IChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/chat")
public class ChatController {
    @Resource
    private IChatService chatService;

    @GetMapping("/listAll")
    public R<List<Chat>> list(HttpServletRequest request){
        HttpSession session = request.getSession();
        Long userId = (Long)session.getAttribute("user");

        LambdaQueryChainWrapper<Chat> chatWrapper = chatService.lambdaQuery();
        chatWrapper.eq(Chat::getUserId,userId);
        chatWrapper.orderByAsc(Chat::getTimestamp);

        List<Chat> chatList =  chatWrapper.list();

        return R.success(chatList);
    }


    @GetMapping("/send")
    public R<Chat> sendMessage(HttpServletRequest request,@RequestParam String question){
        HttpSession session = request.getSession();
        Long userId = (Long)session.getAttribute("user");
        return chatService.sendMessage(userId,question);
    }
}
